/**
 * @description user view 路由
 * @author 高
 */

const { loginRedirect } = require("../../middlewares/loginChecks");

const router = require("koa-router")();

/**
 * 获取登录信息
 * @param {object} ctx ctx
 */
function getUserInfo(ctx) {
  let data = {
    isLogin: false // 默认未登录
  };

  // 如果用户已经登录
  const userInfo = ctx.session.userInfo;
  if (userInfo) {
    data = {
      isLogin: true,
      userName: userInfo.userName
    };
  }
  return data;
}

router.get("/login", async (ctx, next) => {
  await ctx.render("login", getUserInfo(ctx));
});

router.get("/register", async (ctx, next) => {
  await ctx.render("register", getUserInfo(ctx));
});

router.get("/setting", loginRedirect, async (ctx, next) => {
  await ctx.render("setting", ctx.session.userInfo);
});

module.exports = router;